<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Introduction to Java SWT</title>
<link rel="stylesheet" href="/cfg/format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="GUI, SWT, Java, Introduction, Linux, programming">
<meta name="description" content="This chapter of the Java SWT tutorial is an introduction to Java SWT.">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

<script type="text/javascript" src="/lib/jquery.js"></script>
<script type="text/javascript" src="/lib/common.js"></script>

</head>

<body>

<div class="container2">

<div id="wide_ad" class="ltow">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* 160x600, August 2011 */
google_ad_slot = "2484182563";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<div class="content2">


<a href="/" title="Home">Home</a>&nbsp;
<a href="..">Contents</a>


<h1>Introduction to SWT</h1>

<p>
In this part of the Java SWT programming tutorial, we will introduce the Java SWT library and
create our first programs.
</p>

<div class="big_hor">
<script type="text/javascript"><!--
google_ad_client = "ca-pub-9706709751191532";
/* big_horizontal */
google_ad_slot = "2904953388";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>


<p>
The purpose of this tutorial is to get you started with the Java SWT toolkit. Images used in this
tutorial can be downloaded <a href="/img/gui/swt/images.zip">here</a>. I used some icons from the
tango icons pack of the Gnome project. 
</p>

<h2>About</h2>

<p>
The <b>Standard Widget Toolkit</b> (SWT) is a graphical widget 
toolkit for the Java programming language. It was originally developed by IBM. 
It is an alternative to the Java Swing GUI toolkit provided by Sun Microsystems, which 
is a part of the Java Platform, Standard Edition. SWT is written in Java. 
It uses the native GUI APIs like Winapi, GTK+ to create its widgets via the JNI. 
</p>


<h2>Centering a window</h2>

<p>
In the first example, we create a simple window. The window is
centered on the screen. 
</p>

<pre class="code">
package com.zetcode;

import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
 * ZetCode Java SWT tutorial
 *
 * In this program, we show a window in
 * the center of the screen
 *
 * @author jan bodnar
 * website zetcode.com
 * last modified June 2009
 */

public class SWTApp {


    public SWTApp(Display display) {
       
        Shell shell = new Shell(display);
        shell.setText("Center");
        shell.setSize(250, 200);

        center(shell);

        shell.open();

        while (!shell.isDisposed()) {
          if (!display.readAndDispatch()) {
            display.sleep();
          }
        }
    }


    public void center(Shell shell) {

        Rectangle bds = shell.getDisplay().getBounds();

        Point p = shell.getSize();

        int nLeft = (bds.width - p.x) / 2;
        int nTop = (bds.height - p.y) / 2;

        shell.setBounds(nLeft, nTop, p.x, p.y);
    }


    public static void main(String[] args) {
        Display display = new Display();
        new SWTApp(display);
        display.dispose();
    }
}
</pre>

<p>
This example shows a 250x200px window in the centre of the screen.
In each SWT application, there are two important classes. The <code>Display</code>
and the <code>Shell</code>.
The Display is the connection between the SWT and the underlying OS. It implements the event loop
and provides information about the OS. The Shell represents a window. There are top level shells. 
These take a Display as a parent. Other shells are called secondary shells.
</p>

<pre class="explanation">
Shell shell = new Shell(display);
</pre>

<p>
Top level window is created. 
</p>

<pre class="explanation">
shell.setText("Center");
</pre>

<p>
We set a title for the window.  
</p>

<pre class="explanation">
shell.setSize(250, 200);
</pre>

<p>
Here we set a size for the shell/window. 
</p>

<pre class="explanation">
shell.open();
</pre>

<p>
The window is shown on the screen. 
</p>

<pre class="explanation">
while (!shell.isDisposed()) {
  if (!display.readAndDispatch()) {
    display.sleep();
  }
}
</pre>

<p>
These lines start the event mainloop.
</p>

<pre class="explanation">
Rectangle bds = shell.getDisplay().getBounds();
</pre>

<p>
We get the resolution of the screen. If you work with more than
one display, you might need to call the <code>getMonitor()</code> method instead
of <code>getDisplay()</code>. 
</p>

<pre class="explanation">
int nLeft = (bds.width - p.x) / 2;
int nTop = (bds.height - p.y) / 2;
</pre>

<p>
We calculate the left and top coordinates of the window. 
</p>

<pre class="explanation">
shell.setBounds(nLeft, nTop, p.x, p.y);
</pre>

<p>
We set the shell's bounds. 
</p>

<pre class="explanation">
Display display = new Display();
</pre>

<p>
<code>Display</code> is created. 
</p>

<pre class="explanation">
new SWTApp(display);
</pre>

<p>
We instantiate our example program.
</p>

<pre class="explanation">
display.dispose();
</pre>

<p>
After the application terminates, we release the OS resources. 
</p>


<h2>Creating a Tooltip</h2>

<p>
The second example will show a tooltip. A tooltip is a small rectangular window, which
gives a brief information about an object. It is usually a GUI component. It is part of
the help system of the application. 
</p>

<pre class="code">
package com.zetcode;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
 * ZetCode Java SWT tutorial
 *
 * In this program, we show a tooltip
 * 
 * @author jan bodnar
 * website zetcode.com
 * last modified June 2009
 */

public class SWTApp {


    public SWTApp(Display display) {
        
        Shell shell = new Shell(display);

        shell.setText("Tooltip");
        shell.setLocation(300, 300);
        shell.setToolTipText("This is a window");

        shell.setSize(250, 200);

        shell.open();

        while (!shell.isDisposed()) {
          if (!display.readAndDispatch()) {
            display.sleep();
          }
        }        
    }

    public static void main(String[] args) {
        Display display = new Display();
        new SWTApp(display);
        display.dispose();
    }
}
</pre>

<p>
The example creates a window. If we hover a mouse pointer over the area of the
window, a tooltip pops up. 
</p>


<pre class="explanation">
shell.setToolTipText("This is a window");
</pre>

<p>
This line creates a tooltip for the window.
</p>


<img src="/img/gui/swt/tooltip.png" alt="Tooltip">
<div class="figure">Figure: Tooltip</div>


<h2>Quit button</h2>

<p>
In the last example of this section, we will create a quit button. 
When we press this button, the application terminates. 
</p>

<pre class="code">
package com.zetcode;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
 * ZetCode Java SWT tutorial
 *
 * This example shows a button on a window.
 * Clicking on the button, we terminate the
 * application.
 *
 * @author jan bodnar
 * website zetcode.com
 * last modified June 2009
 */

public class SWTApp {

    private Shell shell;

    public SWTApp(Display display) {

        shell = new Shell(display);

        shell.setText("Button");

        initUI();

        shell.setSize(250, 200);
        shell.setLocation(300, 300);

        shell.open();

        while (!shell.isDisposed()) {
          if (!display.readAndDispatch()) {
            display.sleep();
          }
        }
    }


    public void initUI() {

        Button quit = new Button(shell, SWT.PUSH);
        quit.setText("Quit");
        quit.setBounds(50, 50, 80, 30);
        
        quit.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                shell.getDisplay().dispose();
                System.exit(0);
            }
        });
    }


    public static void main(String[] args) {
        Display display = new Display();
        new SWTApp(display);
        display.dispose();
    }
}
</pre>

<p>
We use a <code>Button</code> widget. This is a very common widget.
It shows a text label, image or both. 
</p>

<pre class="explanation">
initUI();
</pre>

<p>
We delegate the creation of the user interface to the <code>initUI()</code> method.
</p>

<pre class="explanation">
Button quit = new Button(shell, SWT.PUSH);
</pre>

<p>
<code>Button</code> widget is created. The parent is the shell. The <code>SWT.PUSH</code>
specifies the type of the button.
</p>

<pre class="explanation">
quit.setText("Quit");
quit.setBounds(50, 50, 80, 30);
</pre>

<p>
We set a label and the size for the button.
</p>

<pre class="explanation">
quit.addSelectionListener(new SelectionAdapter() {
    @Override
    public void widgetSelected(SelectionEvent e) {
        shell.getDisplay().dispose();
        System.exit(0);
    }
});
</pre>

<p>
We add a selection listener for the button. When we click on the button, the
<code>widgetSelected()</code> method is called. Inside this method, we
release the OS resources and exit the application. 
</p>

<img src="/img/gui/swt/quitbutton.png" alt="Quit button">
<div class="figure">Figure: Quit button</div>

<p>
This section was an introduction to the Java SWT library.
</p>


<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="..">Contents</a></span> ‡
<span class="botNavItem"><a href="#">Top of Page</a></span>
</div>


<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified June 16, 2009  <span class="copyright">&copy; 2007 - 2013 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</div> <!-- container -->

</body>
</html>
